package com.ruoyi.module.client;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.module.dto.WebRequestDTO;
import com.ruoyi.module.dto.WebResponseDTO;
import com.ruoyi.module.utils.SignUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Administrator
 * @since 2019/4/23 16:47
 */
@Slf4j
public class OpendjClient {

    /**
     * 查询商家已上传商品信息列表接口
     *
     * @param app_key
     * @param app_secret
     * @param token
     * @param bo
     * @return
     */
    @SneakyThrows
    public WebResponseDTO<?> querySkuInfos(String app_key, String app_secret, String token, Object bo) {
        return this.getRequest("pms/querySkuInfos", "1.0", app_key, app_secret, token, bo);
    }


    /**
     * 根据到家商品编码批量更新商家商品编码接口
     *
     * @param app_key
     * @param app_secret
     * @param token
     * @param bo
     * @return
     */
    public WebResponseDTO<?> batchUpdateOutSkuId(String app_key, String app_secret, String token, Object bo) {
        return this.getRequest("pms/sku/batchUpdateOutSkuId", "1.0", app_key, app_secret, token, bo);
    }

    /**
     * 批量同步商品库存
     *
     * @param app_key
     * @param app_secret
     * @param token
     * @param bo
     * @return
     */
    public WebResponseDTO<?> batchUpdateCurrentQtys(String app_key, String app_secret, String token, Object bo) {
        return this.getRequest("stock/batchUpdateCurrentQtys", "1.0", app_key, app_secret, token, bo);
    }

    /**
     * 单个同步商品库存
     *
     * @param app_key
     * @param app_secret
     * @param token
     * @param bo
     * @return
     */
    public WebResponseDTO<?> updateStock(String app_key, String app_secret, String token, Object bo) {
        return this.getRequest("stock/update", "1.0", app_key, app_secret, token, bo);
    }


    public WebResponseDTO<?> queryStockCenter(String app_key, String app_secret, String token, Object bo) {
        return this.getRequest("stock/queryStockCenter", "1.0", app_key, app_secret, token, bo);
    }

    @SneakyThrows
    public WebResponseDTO<?> getRequest(String url, String v, String appKey, String appSecret, String token, Object bo) {
        String timestamp = DateUtil.now();
        String jd_param_json = JSON.toJSONString(bo);

        WebRequestDTO webReqeustDTO = new WebRequestDTO();
        webReqeustDTO.setApp_key(appKey);
        webReqeustDTO.setFormat("json");
        webReqeustDTO.setTimestamp(timestamp);
        webReqeustDTO.setToken(token);
        webReqeustDTO.setV(v);
        webReqeustDTO.setJd_param_json(jd_param_json);

        String sign = SignUtils.getSignByMD5(webReqeustDTO, appSecret);
        log.info("md5 sign:{}", sign);

        String paramStr = "?v=" + v
                + "&format=json"
                + "&app_key=" + appKey
                + "&app_secret=" + appSecret
                + "&token=" + token
                + "&jd_param_json=" + URLUtil.encodeAll(jd_param_json)
                + "&sign=" + sign
                + "&timestamp=" + URLUtil.encodeAll(timestamp);

        String s = HttpUtil.get("https://openapi.jddj.com/djapi/" + url+ paramStr);
        return JSON.parseObject(s, WebResponseDTO.class);
    }
}
